/*
 * Created on Jul 28, 2003
  */
package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.alarms.Alarm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.SalesOrder;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;

/**
 * @author Ravikanth
 */
public class SalesOrderNotificationShowAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/salesOrderNotificationShow.do" ;  // Added by Basha (08/08/2003)

	public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {
		
		Alarm alarm = (Alarm)request.getAttribute( "alarmObj" );
		HashMap errors = new HashMap();
		String salesOrderId = null;

        HttpSession session = request.getSession();

        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        // Added by Basha (08/08/2003)
        if(user == null)
        {
	        session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }
        // Added by Basha (08/08/2003)

			SalesOrder salesOrder = null;
	
			// To load the sales Order Details and Line Item Details
	
			try{
			if( alarm != null && alarm.getId() > 0 ){
	
				salesOrder = SalesOrderDAO.loadSalesOrderByAlarmId( Integer.toString( alarm.getId() ) );

				request.setAttribute( "alarm" , alarm );
				// Added by veeru on 13th April 2004
				salesOrder.setChangeNumber( SalesOrderDAO.loadChangeNo ( salesOrder.getSalesOrderId () ) - 1 );
		
				request.setAttribute( "salesOrder" , salesOrder );
				int customerId = SalesOrderDAO.loadCustomerId(salesOrder.getSalesOrderId());

				
				//ArrayList cancelledList = SalesOrderDAO.loadCancelledOrderLineItems( salesOrder.getSalesOrderId() );
				 ArrayList cancelledList = SalesOrderDAO.loadCancelledOrderLineItemsByCustomerId(salesOrder.getSalesOrderId(),customerId);
				request.setAttribute( "cancelledList" , cancelledList );

				ArrayList changedList = SalesOrderDAO.loadOnlyChangeOrderLineItems( salesOrder.getSalesOrderId() );
				request.setAttribute( "changedList" , changedList );
		
				//ArrayList presentList = SalesOrderDAO.loadOrderLineItems( salesOrder.getSalesOrderId() );
				ArrayList presentList = SalesOrderDAO.loadOrderLineItemsByCustomerId(salesOrder.getSalesOrderId(),customerId);
				request.setAttribute( "presentList" , presentList );
			}
			} catch ( ScalarException se ){
				
				errors.put ( "Error" , "Could not Load the Sales Order Details" );
				request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );

				se.getRootCause().printStackTrace();
			}			
			
		return mapping.findForward( "success" );
	}

}
